package presentation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;

import control.CControl;

public class PGraphPanel extends JPanel implements Observer, DropTargetListener {
	private static final long serialVersionUID = 1L;
	private final int DEFAULT_SAMPLE_COUNT = 100;
	private JComboBox valueSelector;
	private JLabel samplesLabel = new JLabel("Samples:");
	private JButton setButton = new JButton("Set");
	private JTextField sampleCount = new JTextField();
	private JPanel pageStart = new JPanel();
	private CControl cControl;
	private String selectedValue;
	private int sampleCountValue = DEFAULT_SAMPLE_COUNT;
	private JFreeChart chart;
	
	public PGraphPanel(String[] plotableRegisters, Color color ,int graphNumber, CControl cControl) {
		this.cControl = cControl;
		this.setLayout(new BorderLayout());
		this.setBackground(Color.white);

		
		valueSelector = new JComboBox(plotableRegisters);
		valueSelector.setFont(new Font("Arial",Font.PLAIN,12));
		valueSelector.setBackground(Color.white);
		valueSelector.setPreferredSize(new Dimension(150,20));
		
		selectedValue = cControl.getGraphSettingsParameter("monitorGraph" + graphNumber + "Value");
		try {
			if (selectedValue != null) {
				valueSelector.setSelectedItem(selectedValue);
			} else {
				valueSelector.setSelectedIndex(graphNumber);
			}
		} catch (Exception e) {
			valueSelector.setSelectedIndex(graphNumber);
		}
		selectedValue = (String) valueSelector.getSelectedItem();
		
		samplesLabel.setFont(new Font("Arial",Font.PLAIN,12));
		samplesLabel.setForeground(Color.black);
		
		sampleCount.setFont(new Font("Arial",Font.PLAIN,12));
		sampleCount.setHorizontalAlignment(JTextField.RIGHT);
		sampleCount.setPreferredSize(new Dimension(50,20));
		
		setButton.setBackground(Color.white);
		setButton.setForeground(Color.black);
		setButton.setFont(new Font("Arial",Font.PLAIN,12));
		setButton.setPreferredSize(new Dimension(60,20));
		setButton.addActionListener(new ButtonListener());
		
		this.setTransferHandler(new PPlotTransferHandler());
		
		//pageStart.add(valueSelector);
		pageStart.add(samplesLabel);
		pageStart.add(sampleCount);
		pageStart.add(setButton);
		pageStart.setPreferredSize(new Dimension(0,30));
		pageStart.setBackground(Color.white);
		this.add(pageStart,BorderLayout.PAGE_START);
		
		String unit = cControl.getRegisterUnit((String) valueSelector.getSelectedItem());
		try {
			sampleCountValue = Integer.parseInt(cControl.getGraphSettingsParameter("monitorGraph" + graphNumber + "Samples"));
		} catch (NumberFormatException nfe) {}
		sampleCount.setText(sampleCountValue + "");
		String rangeAxisLabel = (unit != null && !unit.equals("")) ? selectedValue + " [" + unit + "]" : selectedValue;
		chart = PChartFactory.createXYGraph("Sample", rangeAxisLabel, sampleCountValue, color);
		PChartFactory.setRangeAxis(chart,cControl.getRegisterBoundaries((String) valueSelector.getSelectedItem()));
		this.add(new ChartPanel(chart),BorderLayout.CENTER);
		
		cControl.addRegisterObserver(selectedValue, this);
	}
	
	public String getSelectedValue() {
		return selectedValue;
	}
	
	public int getSampleCountValue() {
		return sampleCountValue;
	}
	
	public void showSettings(boolean show) {
		pageStart.setVisible(show);
		this.updateUI();
	}
	
	public void changeGraph(String register)
	{
		String unit = cControl.getRegisterUnit((String) valueSelector.getSelectedItem());
		try {
			sampleCountValue = Integer.parseInt(sampleCount.getText());
		} catch (NumberFormatException nfe) {}
		sampleCount.setText(sampleCountValue + "");
		
			// Re-Register at foundation
			cControl.removeRegisterObserver(selectedValue, PGraphPanel.this);
			
			selectedValue = (String) register;
			
			cControl.addRegisterObserver(selectedValue, PGraphPanel.this);
			
			// And update chart
			String rangeAxisLabel = (unit != null && !unit.equals("")) ? selectedValue + " [" + unit + "]" : selectedValue;
			PChartFactory.setRangeAxisLabel(chart, rangeAxisLabel);
			PChartFactory.clearSamples(chart);
			PChartFactory.setRangeAxis(chart,cControl.getRegisterBoundaries((String) valueSelector.getSelectedItem()));
		
		
		PChartFactory.setSampleCount(chart, sampleCountValue);
		
	}
	
	private class ButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String unit = cControl.getRegisterUnit((String) selectedValue);
			try {
				sampleCountValue = Integer.parseInt(sampleCount.getText());
			} catch (NumberFormatException nfe) {}
			sampleCount.setText(sampleCountValue + "");
			
			//if (!selectedValue.equalsIgnoreCase((String) valueSelector.getSelectedItem())) {
				// Re-Register at foundation
				//cControl.removeRegisterObserver(selectedValue, PGraphPanel.this);
				
				//selectedValue = (String) valueSelector.getSelectedItem();
				
				//cControl.addRegisterObserver(selectedValue, PGraphPanel.this);
				
				// And update chart
				String rangeAxisLabel = (unit != null && !unit.equals("")) ? selectedValue + " [" + unit + "]" : selectedValue;
				PChartFactory.setRangeAxisLabel(chart, rangeAxisLabel);
				PChartFactory.clearSamples(chart);
				PChartFactory.setRangeAxis(chart,cControl.getRegisterBoundaries(selectedValue));
				
				PChartFactory.setSampleCount(chart, sampleCountValue);
			}
			

		
	}

	@Override
	public void update(Observable obs, Object arg) {
		PChartFactory.addSample(chart, (Number) arg);
	}

	@Override
	public void dragEnter(DropTargetDragEvent dtde) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dragExit(DropTargetEvent dte) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dragOver(DropTargetDragEvent dtde) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void drop(DropTargetDropEvent dtde) {
		// TODO Auto-generated method stub
		System.out.print("Dropped");
		
	}

	@Override
	public void dropActionChanged(DropTargetDragEvent dtde) {
		// TODO Auto-generated method stub
		
	}
}
